<style>
.pure_table_extra { width: 1100px; margin-top: 5px; }
.pure_table_extra th { height: 25px; line-height: 25px; }
.pure_table_extra img { cursor: pointer; margin-right: 2px; }
.pure_table_extra td { vertical-align: middle; }
</style>

<?php 
$date = date('Y-m-d', time());
$student_id = Utils::get_param('id', 0);

$user = json_decode($_SESSION['admin_user']);
$opts = new stdClass();
$opts->cd = 't.student_id = :v1 AND t.is_cancel = 0';
$opts->cdv = array(':v1' => $student_id);
$enrls = $orm->get('pb_colg_enrollment', 't.id', 'DESC', NULL, NULL, $opts)->d;
?>

<button class="pure-button pure-primary" onclick="edit_enrl(0);">Add</button>
<table class="pure-table pure_table_extra" cellpadding=0 cellspacing=0>
  <thead>
    <tr>
      <th style="display: none; width: 80px;" class="ui-widget-header">Enrollment ID</th>
      <th style="width: 40px;" class="ui-widget-header">Status</th>
      <th style="width: 40px;" class="ui-widget-header">Type</th>
      <th class="ui-widget-header">Program</th>
      <th style="width: 60px;" class="ui-widget-header">From</th>
      <th style="width: 60px;" class="ui-widget-header">To</th>
      <th style="width: 100px;" class="ui-widget-header">Agent</th>
      <th style="width: 100px;" class="ui-widget-header">User</th>
      <th style="width: 60px;" class="ui-widget-header">Date</th>
      <th style="width: 80px;" class="ui-widget-header">&nbsp;</th>
    </tr>
  </thead>
  <tbody id="enrls_body"></tbody>
</table>

<div id="dialog_pt" class="pure-form" style="display: none; text-align: center;">
  <div id="btns_pt">
    <input type="radio" id="unconditional" name="radio" checked="checked"><label for="unconditional">Unconditonal</label>
    <input type="radio" id="conditional" name="radio"><label for="conditional" style="margin-left: -5px;">Conditional</label>
  </div>
  <textarea id="condition_pt" style="display: none; width: 100%; height: 300px; resize: none;"></textarea>
</div>

<script type="text/javascript">
  /*<![CDATA[*/
  var __enrls = JSON.parse('<?php echo addslashes(json_encode($enrls)); ?>');
  var __pturl = '<?php echo ColgUtils::get_pturl($app_data); ?>';
  var __diploma = '<?php echo PT_DIPLOMA; ?>';
  var __language = '<?php echo PT_LANGUAGE; ?>';
  var __ncea = '<?php echo PT_NCEA; ?>';
  var __cd = '- achieving an IELTS score of 6.0 with no band less than 5.5 or equivalent';
  var __user = JSON.parse('<?php echo addslashes(json_encode($user)); ?>');
  
  $(function() {
    $('#btns_pt').buttonset();
    $('#unconditional').on('click', function() {
      $('#condition_pt').val('');
      $('#condition_pt').hide();
    });
    $('#conditional').on('click', function() {
      $('#condition_pt').val(__cd);
      $('#condition_pt').show();
    });
  });

  function print_enrl(id, ev) {
    var obj = Utils.get_by_id(id, __enrls);
    if (obj.program_type == __diploma) {
      $('#unconditional').prop("checked", true);
      $('#condition_pt').val('');
      $('#condition_pt').hide();
      $('#btns_pt').buttonset('refresh');
      
      var func = function() {
        var obj = Utils.get_by_id(id, __enrls);
        var tmpl = 'd_offer';
        window.open(__pturl + '&tmpl=' + tmpl + '&id=' + id + '&cd=' + $('#condition_pt').val());
        $('#dialog_pt').dialog('close');
      };
      
      $('#dialog_pt').dialog({
        width: 500,
        position: {
          my: 'left+0 bottom+0',
          of: ev.target
        },
        buttons: {
          'OK': function() {
            func();
          },
          'Cancel': function() { 
            $(this).dialog('close');
          }
        }
      });
    } else if (obj.program_type == __language) {
      tmpl = 'l_offer';
      window.open(__pturl + '&tmpl=' + tmpl + '&id=' + id);
    } else if (obj.program_type == __ncea) {
      tmpl = 'n_offer';
      window.open(__pturl + '&tmpl=' + tmpl + '&id=' + id);
    }
  };
  
  function render_enrls() {
    $('#enrls_body').empty();
    for (var idx in __enrls) {
      var itm = __enrls[idx];
      $('#enrls_body').append(
        '<tr class="' + ((idx % 2 == 0) ? 'pure-table-even' : 'pure-table-odd') + '">' + 
          '<td style="display: none;">' + itm.track_id + '</td>' +
          '<td>' + ((itm.is_trial == 0) ? (itm.is_paid == 0 ? 'Unpiad' : 'Paid') : 'Trial') + '</td>' +
          '<td>' + itm.program_type + '</td>' +
          '<td title="' + itm.program_name + '">' + Utils.get_short(itm.program_name, 60) + '</td>' +
          '<td>' + itm.start_academic + '</td>' +
          '<td>' + itm.end_academic + '</td>' +
          '<td>' + itm.agent_title__ + '</td>' +
          '<td>' + itm.user_title__ + '</td>' +
          '<td>' + itm.date + '</td>' +
          '<td>' + 
            '<span style="cursor: pointer; float: left;" class="ui-button-icon-primary ui-icon ui-icon-pencil" onclick="edit_enrl(' + itm.id + ');"></span>' +
            '<span style="cursor: pointer; float: left;" class="ui-button-icon-primary ui-icon ui-icon-print" onclick="print_enrl(' + itm.id + ', event);"></span>' +
            ((itm.is_paid == 0) ? '<span style="cursor: pointer; float: left;" class="ui-button-icon-primary ui-icon ui-icon-closethick" onclick="cancel_enrl(' + itm.id + ');"></span>' : '') +
            '</td>' + 
        '</tr>'
      );
    }
    if (__enrls.length == 0) {
      $('#enrls_body').append('<tr class="pure-table-even"><td colspan=10>None</td></tr>');
    }
  };

  function cancel_enrl(id) {
    var obj = Utils.get_by_id(id, __enrls);
    obj.is_cancel = 1;
    Ajax.request(
      __requrl + 'submit_enrl',
      'req=' + Utils.encodeURI(JSON.stringify(obj)),
      function(rslt) {
        Utils.splice_by_id(id, __enrls);
        render_enrls();
        $('#dialog_enrl').dialog('close');
      },
      { confirm: 1 }
    );
  };

  function edit_enrl(id) {
    var func = function() {
      obj.is_trial = $('#trial').is(':checked') ? 1 : 0;
      obj.agent_id = $('#agent option:selected').val();
      obj.agent_title__ = $('#agent option:selected').text();
      obj.reg_date = $('#reg_date').val();
      obj.total_academic = $('#total_academic').val();
      obj.start_academic = $('#start_academic').val();
      obj.end_academic = $('#end_academic').val();
      obj.weeks_academic = $('#weeks_academic').val();
      obj.total_accommodation = $('#total_accommodation').val();
      obj.start_accommodation = $('#start_accommodation').val();
      obj.end_accommodation = $('#end_accommodation').val();
      obj.weeks_accommodation = $('#weeks_accommodation').val();
      obj.price_accommodation = $('#price_accommodation').val();
      obj.total_insurance = $('#total_insurance').val();
      obj.start_insurance = $('#start_insurance').val();
      obj.end_insurance = $('#end_insurance').val();
      obj.months_insurance = $('#months_insurance').val();
      obj.papers = $('#papers').val();
      obj.fee_admin = $('#fee_admin').val();
      obj.fee_pickup = $('#fee_pickup').val();
      obj.fee_exam = $('#fee_exam').val();
      obj.fee_homestay = $('#fee_homestay').val();
      obj.fee_living = $('#fee_living').val();
      obj.fee_material = $('#fee_material').val();
      obj.fee_registration = $('#fee_registration').val();
      obj.fee_other = $('#fee_other').val();
      obj.enrl_type_note = $('#enrl_type_note').val();
      obj.note = $('#note').val();

      var value = $("#intakes select").val();
      if (!value) {
        value = '';
      } else {
        value = ',' + value.join(',') + ',';
      }
      obj.intakes__ = value;
      
      Ajax.request(
        __requrl + 'submit_enrl',
        'req=' + Utils.encodeURI(JSON.stringify(obj)),
        function(rslt) {
          var obj = Utils.get_by_id(rslt.id, __enrls);
          if (!obj) {
            __enrls.splice(0, 0, rslt);
          }
          render_enrls();
          $('#dialog_enrl').dialog('close');
        },
        null
      );
    };
    
    $('#dialog_enrl').dialog({
      buttons: {
        'OK': function() {
          func();
        },
        'Cancel': function() { 
          var r = window.confirm('Are you sure?');
          if (r) {
            $(this).dialog('close');
          }
        }
      },
      width: 1200
    });

    var obj = Utils.get_by_id(id, __enrls);
    if (!obj) {
      obj = {
        id: 0,
        date: __date,
        user_id: __user.id,
        user_title__: __user.first_name + ' ' + __user.last_name,
        agent_id: 0,
        agent_title__: '',
        student_id: __data[0].id,
        student_title__: __data[0].title__,
        enrolled_program_id: 0,
        program_id: 0,
        program_name: '',
        program_type: 'Diploma',
        reg_date: Utils.date_to_str(),
        total_academic: 0,
        start_academic: Utils.date_to_str(),
        end_academic: Utils.date_to_str(),
        weeks_academic: 0,
        total_accommodation: 0,
        start_accommodation: Utils.date_to_str(),
        end_accommodation: Utils.date_to_str(),
        weeks_accommodation: 0,
        price_accommodation: 250.00,
        total_insurance: 0,
        start_insurance: Utils.date_to_str(),
        end_insurance: Utils.date_to_str(),
        months_insurance: 0,
        intakes__: '',
        papers: 0,
        fee_admin: 0,
        fee_pickup: 0,
        fee_exam: 0,
        fee_homestay: 0,
        fee_living: 0,
        fee_material: 0,
        fee_registration: 0,
        fee_other: 0,
        enrl_type_note: 'Fulltime',
        note: '',
        is_paid: 0,
        is_trial: 0
      };
      if (____enrlprograms.length > 0) {
        obj.enrolled_program_id = ____enrlprograms[0].id;
        obj.program_id = ____enrlprograms[0].program_id;
        obj.program_name = ____enrlprograms[0].__name;
        obj.program_type = ____enrlprograms[0].__type;
      } else {
        obj.enrolled_program_id = 0;
        obj.program_id = ____programs[0].id;
        obj.program_name = ____programs[0].name;
        obj.program_type = ____programs[0].type;
      }
    }

    ____enrl(obj, null);
  };
  /*]]>*/
</script>